//login.js
//获取应用实例
var app = getApp()
import { formatParams } from "../../utils/util.js";
import { change } from "../../utils/check.js";
import { showTi } from "../../utils/modal.js"
var sliderWidth = 40;
Page({
  data: {
    login: {
      login: '',
      password: '',
      code: '',
      mp_open_id: '',
    },
    activeIndex: 1,
    activeIndexTwo: 1,
    issend: false,
    time: 60
  },
  onLoad: function () {
    let self = this
    app.getOpenId(id => {
      self.data.login.mp_open_id = id;
    })
  },
  bindKeyInputLog: function (e) {
    this.data.login.login = e.detail.value
  },
  bindKeyInputPwd: function (e) {
    this.data.login.password = e.detail.value
  },
  bindKeyInputCode: function (e) {
    this.data.login.code = e.detail.value
  },
  //登录
  login: function () {
    let self = this
    self.data.login.role = 2;
    self.data.login.ref = '服务商';
    let params = JSON.parse(JSON.stringify(self.data.login))
    console.log(self.data.activeIndex)
    if (self.data.activeIndex == 1) {
      delete self.data.login.code
      app.query('web/sign_in', self.data.login, 'post').then(res => {
        console.log(res)
        if (res.code === 200) { 
          app.globalData.userInfo = res.user
          wx.setStorageSync('wechat_service_5mutian_token', res.token)
          wx.setStorageSync('wechat_user', res.user)
          console.log(wx.getStorageSync('wechat_user'))
          if (res.user.user_type === 'driver' && res.user.audit == true){
            console.log("jinalae")
            wx.redirectTo({ url: '../transport/transport' })
          }
          if (res.user.user_type === '' && res.user.audit==true){
            wx.switchTab({ url: '../robsingle/robsingle' })
          }
          if (res.user.audit == false){
            showTi("你的账号正在审核中,请耐心等待")
          } 
        }else{
          showTi(res.msg)
        }
      })
    }
    if (self.data.activeIndexTwo != 1) {
      delete self.data.login.password
      app.query('web/login_code', self.data.login, 'post').then(res => {
        if (res.code === 200) {
          console.log(res);
          app.globalData.userInfo = res.user
          wx.setStorageSync('wechat_service_5mutian_token', res.token)
          if (res.user.user_type === 'driver' && res.user.audit == true) {
            wx.redirectTo({ url: '../transport/transport' })
          }
          if (res.user.user_type === '' && res.user.audit == true) {
            wx.switchTab({ url: '../robsingle/robsingle' })
          } 
          if (res.user.audit == false) {
            showTi("你的账号正在审核中,请耐心等待")
          } 
        }else {
          showTi(res.msg)
        }
      })
    }

  },
  //发送验证码
  send: function () {
    if (!/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.data.login.login)){
      showTi("请输入正确的手机号码")
    }else{
      app.query('web/send_code', { tel: this.data.login.login }, 'post').then(res => {
        if (res.code === 200) {
          console.log(res);
          this.timeDown();
        } else {
          showTi(res.msg)
        }
      })
    }
  },
  //验证码倒计时
  timeDown: function () {
    this.setData({ "issend": true })
    let time = 60;
    let timer = setInterval(() => {
      --time;
      this.setData({ "time": time })
      if (time <= 0) {
        this.setData({ "issend": false })
        clearInterval(timer)
      }
    }, 1000)
  },
  //点击切换
  tabClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.id,
      activeIndexTwo: e.currentTarget.id
    });
    console.log(e.currentTarget.id)
  },
  //点击切换
  activeClick: function (e) {
    this.setData({
      activeIndex: e.currentTarget.id,
      activeIndexTwo: e.currentTarget.id
    });
    console.log(e.currentTarget.id)
  },
  register:function(){
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }

})
